package com.nuc.iblog.controler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nuc.iblog.entity.User;
import com.nuc.iblog.entity.UserBlog;
import com.nuc.iblog.jpa.UserBlogJpa;
import com.nuc.iblog.service.ClUserService;
import com.nuc.iblog.service.UserBlogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Tyranitarx on 2018/1/12.
 *
 * @Description :
 */
@Controller
@RequestMapping("cl")
public class CLUserController {
    Logger log = LoggerFactory.getLogger(CLUserController.class);
    @RequestMapping("/login")
    public String toPage(HttpServletRequest request){
        return "cl/login";
    }

    @Autowired
    private ClUserService clUserService;
    private User user;
    private Map<String,Object> returnMap;
    @Autowired
    private UserBlogService userBlogService;
    @ResponseBody
    @RequestMapping("/doLogin")
    public Map<String,Object> doLogin(@RequestBody String json, HttpServletRequest request){
        returnMap=new HashMap<String,Object>();
        JSONObject object=JSON.parseObject(json);
        log.info("用户登录获取到的json为:"+json);
        user= clUserService.Login(object.getString("username"),object.getString("password"));
        if(user!=null){
            request.getSession().setAttribute("User",user);
            returnMap.put("code","1");
            returnMap.put("msg","登录成功");
            return returnMap;
        }
        else
        {
            returnMap.put("code","0");
            returnMap.put("msg","用户名或密码错误");
            return returnMap;
        }
    }
    private int status;
    @ResponseBody
    @RequestMapping("/doRegist")
    public Map<String, Object> doRegist(@RequestBody String json,HttpServletRequest request){
        log.info("用户注册");
        returnMap=new HashMap<String,Object>();
        JSONObject object=JSON.parseObject(json);
        user= clUserService.Regist(1,object.getString("username"),object.getString("nickname"),"男",object.getString("password"));
        if(user==null){
                returnMap.put("code","0");
                returnMap.put("msg","用户名已存在");
                return returnMap;
        }else {
            UserBlog userBlog=new UserBlog();
            userBlog.setUser(user);
            userBlog.setBlogpicurl("blogFile/defaultblog.jpg");
            userBlog.setArticlenum(1);
            userBlogService.ModifyBlog(userBlog);
            returnMap.put("code","1");
            returnMap.put("msg","注册成功");
            return returnMap;
        }
    }
    @ResponseBody
    @RequestMapping("/changeSelf")
    public Map<String,Object> changeSelf(@RequestBody String json,HttpServletRequest request){
        returnMap=new HashMap<String,Object>();
        log.info(json);
        JSONObject object=JSON.parseObject(json);
        user=(User)request.getSession().getAttribute("User");
        int uid=user.getUid();
        String nickname=object.getString("nickname");
        String usex=object.getString("usex");
        String usign=object.getString("usign");
        String uheadpicurl=object.getString("uheadpicurl");
        user=clUserService.change(uid,nickname,usign,usex,uheadpicurl);
        returnMap.put("code","1");
        request.getSession().setAttribute("User",user);
        return returnMap;
    }
    @RequestMapping("/exitLogin")
    public String exitLogin(HttpServletRequest request){
        request.getSession().setAttribute("User",null);
        return "redirect:/cl/firstpage";
    }
    @RequestMapping("/toRegist")
    public String toRegist(){
        return "/cl/regist";
    }
}
